package cn.tedu.carehome.foundation.service.impl;

import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.mapper.FoodMapper;
import cn.tedu.carehome.foundation.mapper.FoodPackageLinkMapper;
import cn.tedu.carehome.foundation.mapper.FoodPackageMapper;
import cn.tedu.carehome.foundation.mapper.OlderMapper;
import cn.tedu.carehome.foundation.pojo.dto.*;
import cn.tedu.carehome.foundation.pojo.entity.Food;
import cn.tedu.carehome.foundation.pojo.entity.FoodPackage;
import cn.tedu.carehome.foundation.pojo.entity.OlderFoodPackage;
import cn.tedu.carehome.foundation.pojo.vo.*;
import cn.tedu.carehome.foundation.restful.JsonPage;
import cn.tedu.carehome.foundation.service.IFoodPackageService;
import cn.tedu.carehome.foundation.service.IFoodService;
import cn.tedu.carehome.foundation.web.ServiceCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class FoodPackageServiceImpl implements IFoodPackageService {
    @Autowired
    private FoodPackageMapper foodPackageMapper;

    @Autowired
    private OlderMapper olderMapper;

    @Autowired
    private FoodPackageLinkMapper foodPackageLinkMapper;


    @Override
    public void addNew(FoodPackageAddNewDTO foodPackageAddNewDTO){
        log.debug("开始处理【添加套餐】的业务，参数：{}", foodPackageAddNewDTO);
        // 检查套餐名称是否已经被占用
        String name = foodPackageAddNewDTO.getName();
        int countByName = foodPackageMapper.countByName(name);
        if (countByName > 0) {
            // 如果已经被占用，抛出异常
            String message = "添加套餐失败，套餐名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        FoodPackage foodPackage=new FoodPackage();
        BeanUtils.copyProperties(foodPackageAddNewDTO,foodPackage);
        log.debug("即将插入数据，参数：{}", foodPackage);
        foodPackageMapper.insert(foodPackage);
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除套餐】的业务，编号参数：{}", id);
        int countById= foodPackageMapper.countById(id);
        if(countById==0){
            String message = "删除套餐失败,套餐id不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //看看套餐有没有关联餐品
        int countFood=foodPackageMapper.countFoodByFoodPackageId(id);
        if(countFood>0){
            String message = "删除套餐失败,套餐关联了餐品!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        foodPackageMapper.deleteById(id);


    }

    @Override
    public List<FoodPackageAdminVO> queryFoodPackageList() {
        log.debug("开始处理【查询套餐列表】的业务，无参数");
        return foodPackageMapper.selectAdmin();
    }

    @Override
    public JsonPage<FoodPackageAdminVO> queryFoodPackageListByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<FoodPackageAdminVO> list = foodPackageMapper.selectAdminByPage();
        return JsonPage.restPage(new PageInfo<>(list));
    }


    @Override
    public FoodPackageUpdateVO selectById(Long id) {
        log.debug("开始处理【根据套餐id查询餐品】的业务，参数id{}",id);
        return foodPackageMapper.selectById(id);
    }

    @Override
    public void update(FoodPackageUpdateDTO foodPackageUpdateDTO) {
        log.debug(String.valueOf(foodPackageUpdateDTO));
        log.debug("开始处理【根据套餐id修改套餐】的业务，参数{}",foodPackageUpdateDTO);
        // 检查套餐id是否存在
        int countById= foodPackageMapper.countById(foodPackageUpdateDTO.getId());
        if(countById==0){
            String message = "修改套餐失败,套餐id不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        //检查修改后的餐品名字是否和已有的餐品重名
        Long id=foodPackageUpdateDTO.getId();
        String name=foodPackageUpdateDTO.getName();
        int count= foodPackageMapper.countByNameAndNotId(name,id);
        if(count>0){
            String message = "修改套餐失败,该名字有其他餐品用过了!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        foodPackageMapper.update(foodPackageUpdateDTO);
    }

    /**
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     */

    @Override
    public void addNewLink(OlderFoodPackageAddNewDTO olderFoodPackageAddNewDTO) {
        log.debug("开始处理【添加老人套餐关联】的业务，参数：{}", olderFoodPackageAddNewDTO);

        //检查老人是否存在
        OlderStandardVO olderStandardVO = olderMapper.getStandardById(olderFoodPackageAddNewDTO.getOlderId());
        if (olderStandardVO == null){
            String message = "增加关联失败,老人不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //检查套餐是否存在
        int foodPackageCount = foodPackageMapper.countById(olderFoodPackageAddNewDTO.getFoodPackageId());
        if (foodPackageCount==0){
            String message = "增加关联失败,该套餐不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //检查该老人是否已经有了套餐关联
        OlderFoodPackageStandardVO olderFoodPackageStandardVO = foodPackageMapper.selectLinkStandardByOlder(olderFoodPackageAddNewDTO.getOlderId());
        if (olderFoodPackageStandardVO!=null){
            String message = "增加关联失败,["+olderFoodPackageStandardVO.getOlderName()+"]已经关联套餐了!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //开始添加
        OlderFoodPackage olderFoodPackage=new OlderFoodPackage();
        BeanUtils.copyProperties(olderFoodPackageAddNewDTO,olderFoodPackage);
        int rows = foodPackageMapper.insertLink(olderFoodPackage);
        if (rows != 1) {
            String message = "添加关联失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void deleteLinkById(Long id) {
        //检查是否存在该关联信息
        OlderFoodPackageStandardVO olderFoodPackageStandardVO = foodPackageMapper.selectLinkStandardById(id);
        if (olderFoodPackageStandardVO==null){
            String message = "删除关联失败，关联数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        };
        //执行删除
        int rows = foodPackageMapper.deleteLinkById(id);
        if (rows != 1) {
            String message = "删除关联失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public List<OlderFoodPackageLinkListItemVO> selectLink() {
        List<OlderFoodPackageLinkListItemVO> list= foodPackageMapper.selectLink();
        return list;
    }

    @Override
    public void updateLink(OlderFoodPackageUpdateDTO olderFoodPackageUpdateDTO) {
        /**
         * 判断关联是否存在
         */
        OlderFoodPackageStandardVO olderFoodPackageStandardVO = foodPackageMapper.selectLinkStandardById(olderFoodPackageUpdateDTO.getId());
        if (olderFoodPackageStandardVO==null){
            String message = "删除关联失败，关联数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //检查套餐是否存在
        int foodPackageCount = foodPackageMapper.countById(olderFoodPackageUpdateDTO.getFoodPackageId());
        if (foodPackageCount==0){
            String message = "修改关联失败,该套餐不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //检查老人是否存在
        OlderStandardVO olderStandardVO = olderMapper.getStandardById(olderFoodPackageUpdateDTO.getOlderId());
        if (olderStandardVO == null){
            String message = "修改关联失败,老人不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //执行修改
        int rows = foodPackageMapper.updateLink(olderFoodPackageUpdateDTO);
        if (rows != 1) {
            String message = "修改关联失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public OlderFoodPackageStandardVO selectStandardLinkById(Long id) {

        OlderFoodPackageStandardVO olderFoodPackageStandardVO=foodPackageMapper.selectLinkStandardById(id);
        if (olderFoodPackageStandardVO==null){
            String message = "查询关联失败，不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return olderFoodPackageStandardVO;
    }

    @Override
    public List<FoodPackageFoodStandardVO> selectFoodByFoodPackage(Long id) {
        //检查id是否存在
        int count = foodPackageMapper.countById(id);
        if (count==0){
            String message = "查询失败，套餐id不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //执行查询
        List<FoodPackageFoodStandardVO> list = foodPackageLinkMapper.selectFoodByFoodPackageId(id);
        return list;
    }
}
